package com.keshan.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.keshan.pojo.*;
import com.keshan.service.BloodOxygenServices;
import com.keshan.service.UserDeviceServices;
import com.keshan.utils.DateUtil;
import com.keshan.utils.JWTUtils;
import com.keshan.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author keshan
 * @date 2022/10/22 - 10-24
 */
@CrossOrigin
@Controller
@RequestMapping("/bloodoxygen")
public class BloodOxygenController {
    @Autowired
    BloodOxygenServices bloodOxygenServices;
    @Autowired
    UserDeviceServices userDeviceServices;

    /**
     * 插入所有的数据
     *
     * @param bloodOxygenJson
     * @return
     */
    @RequestMapping("/insertbloodoxygen")
    @ResponseBody
    public Msg insertBySelectiveToBloodOxygen(@RequestBody BloodOxygenJson bloodOxygenJson) {
        Date date = new Date();
        System.out.println(bloodOxygenJson.toString());
        List<UserDevice> userDeviceList = userDeviceServices.getUserDeviceListByImei(bloodOxygenJson.getImei());
        try {
            BloodOxygen bloodOxygen = new BloodOxygen();
            bloodOxygen.setBloodoxygen(bloodOxygenJson.getBloodoxygen());
            bloodOxygen.setBloodoxygenH(bloodOxygenJson.getBloodoxygen_h());
            bloodOxygen.setBloodoxygenL(bloodOxygenJson.getBloodoxygen_l());
            bloodOxygen.setTimeBegin(DateUtil.StringToDate(bloodOxygenJson.getTime_begin_str()));
            bloodOxygen.setDeviceType(bloodOxygenJson.getDevice_type());
            bloodOxygen.setUsername(userDeviceList.get(0).getUsername());
            int result = bloodOxygenServices.insertBySelectiveBloodOxygen(bloodOxygen);
            return Msg.success().add("result", result);

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return Msg.failInsert();
    }


    /**
     * 查询今日的隐藏数据
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping("/bloodoxygenlisthidetoday")
    @ResponseBody
    public Msg selectBloodOxygenListHideByImeiToday(HttpServletRequest httpServletRequest) {
        Map<String, String> map = new HashMap<>();
        String token = httpServletRequest.getHeader("token");
        System.out.println(token);
        try {
            DecodedJWT decodedJWT = JWTUtils.verifyAndInfo(token);
            String username = decodedJWT.getClaim("username").asString();
            System.out.println(username);
//            List<UserDevice> userDeviceList = userDeviceServices.getUserDeviceListByUsername(username);
            List<BloodOxygenHide> bloodOxygenList = bloodOxygenServices.getBloodOxygenListHideByUsernameToday(username);
            return Msg.success().add("bloodOxygenList", bloodOxygenList);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Msg.fail().add("error", "查询失败");
    }

    @RequestMapping("/bloodoxygenlistdayavg")
    @ResponseBody
    public Msg selectBloodOxygenListByImeiDayAvg(HttpServletRequest httpServletRequest) {
        Map<String, String> map = new HashMap<>();
        String token = httpServletRequest.getHeader("token");
        System.out.println(token);
        try {
            DecodedJWT decodedJWT = JWTUtils.verifyAndInfo(token);
            String username = decodedJWT.getClaim("username").asString();
            System.out.println(username);
            List<BloodOxygenDayAvg> bloodOxygenDayAvgList = bloodOxygenServices
                    .getBloodOxygenListEverydayAvgMonth(username);
            return Msg.success().add("bloodOxygenDayAvgList", bloodOxygenDayAvgList);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Msg.fail().add("error", "查询失败");
    }

}
